package net.trevize.datasetexplorer;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

import net.trevize.galatee.GEvent;
import net.trevize.galatee.GListener;
import net.trevize.jmagine.Jmagine;
import net.trevize.tinker.SystemCommandHandler2;

/**
 * 
 * 
 * @author Nicolas James <nicolas.james@gmail.com> [[http://njames.trevize.net]]
 * GListenerImpl.java - May 27, 2010
 */

public class GListenerImpl implements GListener {

	class SystemCommandThread extends Thread {
		private SystemCommandHandler2 sch2 = new SystemCommandHandler2();
		private String command;

		public SystemCommandThread(String command) {
			this.command = command;
		}

		public void run() {
			String[] commands = new String[1];
			commands[0] = command;
			sch2.exec(commands);
		}
	}

	class JmagineThread extends Thread {
		private String image_path;

		public JmagineThread(String image_path) {
			this.image_path = image_path;
		}

		public void run() {
			JFrame f = new JFrame();
			f.setTitle("View image: " + image_path);
			JPanel p0 = new JPanel();
			p0.setLayout(new BorderLayout());
			f.getContentPane().add(p0);
			Jmagine jmagine = new Jmagine(image_path);
			p0.add(jmagine, BorderLayout.CENTER);
			f.setSize(512, 512);
			f.setLocationRelativeTo(null);
			f.setVisible(true);
			jmagine.updateScaleToComponentSizeIfLarger();
		}
	}

	@Override
	public void itemDoubleClicked(GEvent e) {
		System.out.println("item doubleclicked ["
				+ e.getSelectedItem().getLocalFilepath() + "]");

		//		SystemCommandThread sct = new SystemCommandThread("display \""
		//				+ e.getSelectedItem().getLocalFilepath() + "\"");
		//		sct.start();

		JmagineThread jmagine_thread = new JmagineThread(e.getSelectedItem()
				.getLocalFilepath());
		jmagine_thread.start();
	}

	@Override
	public void selectionChanged(GEvent e) {
		System.out.println("changing selection for ["
				+ e.getSelectedItem().getLocalFilepath() + "]");
	}

}
